/**
 * Created by admin on 2017/6/23.
 */
var path = require('path');
var utils = require('./utils');
var fse = require('fs-extra');

module.exports = function (dir, option) {
    dir = dir || '.';
    var outputDir = path.resolve(option.output || dir);

    //写入文件
    function outputFile (file,content) {
        console.log('生成页面：%s',file.slice(outputDir.length+1));
        fse.outputFileSync(file,content);
    }

    // 生成文章内容页面
    var sourceDir = path.resolve(dir, '_posts');
    utils.eachSourceFile(sourceDir, function (f, s) {
        var html = utils.renderPost(dir, f);
        var relativeFile = utils.stripExtname(f.slice(sourceDir.length + 1)) + '.html';
        var file = path.resolve(outputDir, 'posts', relativeFile);
        outputFile(file, html);
    });

    // 生成首页
    var htmlIndex = utils.renderIndex(dir);
    var fileIndex = path.resolve(outputDir, 'index.html');
    outputFile(fileIndex, htmlIndex);
}

